package com.example.gustavo.hello.activity;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpClientStack;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.gustavo.hello.R;
import com.example.gustavo.hello.net.OkHttpStack;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.ResponseHandlerInterface;
import com.loopj.android.http.TextHttpResponseHandler;
import com.squareup.okhttp.OkHttpClient;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

import retrofit.client.OkClient;

public class ListViewActivity extends AppCompatActivity {


    private static final String TAG = "ListViewActivity";
    private String url1 = "http://api.ktkt.com/";
    private String url2 = "http://apit.liangtou.com/";
    private AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            loadA();
            loadB();
            if(!stop){
                handler.postDelayed(runnable, 1000);
            }

        }
    };
    private Handler handler = new Handler();
    private boolean stop;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        handler.post(runnable);



    }

    private AsyncHttpClient getHttpClient(){
        /*if (asyncHttpClient == null) {
            synchronized (this){
                if (asyncHttpClient == null) {
                    asyncHttpClient = new AsyncHttpClient();

                }
            }
        }*/
        return asyncHttpClient;
    }

    private void loadA(){

        post(this, url1, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {

                if(responseString.length() > 3000){
                    responseString = responseString.substring(0, 3000);
                }
                if(!responseString.contains("message")){
                    Log.w(TAG, "request=" + getRequestURI().toString() + " responseString=" + responseString);
                    throw new RuntimeException("ktkt mistake url liangtou");
                }else{
                    Log.i(TAG, "ok");
                }
            }
        });

    }
    private void loadB(){

        post(this, url2, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {

                if(responseString.length() > 3000){
                    responseString = responseString.substring(0, 3000);
                }
                if(responseString.contains("message")){
                    Log.w(TAG, "request=" + getRequestURI().toString() + " responseString=" + responseString);
                    throw new RuntimeException("liangtou mistake url ktkt");
                }else{
                    Log.i(TAG, "ok");

                }
            }
        });

    }


    private void post(Context context, String url, ResponseHandlerInterface responseHandlerInterface) {
        getHttpClient().post(context, url, null, responseHandlerInterface);
    }

    @Override
    protected void onPause() {
        super.onPause();

        stop = true;
    }
}
